/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.model.service;

import com.model.domain.dinero.Partida;
import com.model.domain.ubicacion.Conglomerado;
import com.model.domain.ubicacion.AreaGeografica;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.www.jdbc.Select2Jdbc;
import util.www.jdbc.SeteadorJdbc;


/**
 *
 * @author Rdelacruz
 */
public class PartidaService 
extends AbstractService
{
    
    public List<Partida> findPartidasDiarias(Connection con, Conglomerado filtro) throws SQLException { 
        String cond = "";
        switch(filtro.getAreaGeografica()) {
            case URBANO:
            case RESTO_URBANO:
                cond = " and CodPartida<>'232122R'";
            break;    
            case RURAL:
                cond = " and CodPartida<>'232122U'";
        }        
        return getSelect("where EsDiario=1"+cond).getLista(con);
    }

    public List<Partida> findPartidasNoDiarias(Connection con) throws SQLException {
        return getSelect("where EsDiario=0").getLista(con);
    }
    
    private Select2Jdbc getSelect(String where) throws SQLException {
        Select2Jdbc select = new Select2Jdbc(Partida.class);
        select.setSelect("select *");
        select.setFrom("from Partida");
        select.setOrderby("order by orden");
        select.setWhereHaving(where); 
        
        select.agregarSeteador(new SeteadorJdbc<Partida>() {
            @Override
            public void setear(Connection con, ResultSet rs, Partida obj) throws SQLException {
                obj.setCodigo(rs.getString("CodPartida"));
                obj.setNombre(rs.getString("NomPartida"));
                obj.setAbreviatura(rs.getString("AbrPartida"));
                obj.setDiario(rs.getBoolean("EsDiario"));
            }
        });        
        return select;
    }

    public List<Partida> findAll(Connection con) throws SQLException {
        List<Partida> lista = new ArrayList<Partida>();
        Statement st = null;
        try {                        
            st = con.createStatement();
            String sql = "select * from Partida order by orden";
            ResultSet rs = st.executeQuery(sql);            
            while(rs.next()) {
                Partida obj = new Partida();
                obj.setCodigo(rs.getString("CodPartida"));
                obj.setNombre(rs.getString("NomPartida"));
                obj.setAbreviatura(rs.getString("AbrPartida"));
                obj.setDiario(rs.getBoolean("EsDiario"));
                lista.add(obj);
            }
        }
        finally {
            if(st!=null) st.close();
        }
        return lista;
    }

    public Partida findByCodigo(Connection con, String codigo) throws SQLException {
        Statement st = null;
        try {                        
            st = con.createStatement();
            String sql = "select * from Partida where CodPartida='"+codigo+"'";
            ResultSet rs = st.executeQuery(sql);            
            if(rs.next()) {
                Partida obj = new Partida();
                obj.setCodigo(rs.getString("CodPartida"));
                obj.setNombre(rs.getString("NomPartida"));
                return obj;
            }
        }
        finally {
            if(st!=null) st.close();
        }
        return null;
    }

    
    
    
}
